#!/usr/bin/python
# -*- coding:UTF-8 -*-

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import memcache
from google.appengine.runtime import apiproxy_errors
from google.appengine.api import mail
import os
import logging
import random
import time
from model import *

class Admin(webapp.RequestHandler):

    def post(self):
        self.get()
          
    def get(self):
        sessionkey=self.request.cookies.get("sessionkey","NULL")
        addr=self.request.remote_addr
        session = db.GqlQuery ("SELECT * FROM Session WHERE sessionkey=:1 AND addr=:2",sessionkey,addr)
        logged=False
        email="null"
        for s in session:
            if s.expire==False:
                logged=True
                email=s.email
        if logged==False:
            self.redirect("/login")
        
        template_values = {"email":email}
        
        path = os.path.join(os.path.dirname(__file__), 'trmplates/admin/admin.html')
        self.response.out.write(template.render(path,template_values))

def main():
    application = webapp.WSGIApplication([
                                          ('/admin',Admin)
                                          ],debug=True)
    run_wsgi_app(application)

if __name__ == "__main__":
    main()